import React from 'react';
import { Button } from 'antd';

export default () => {
    const print = () => {
        // const el = document.getElementById('helo');
        const iframe = document.createElement('IFRAME');
        let doc = null;
        iframe.setAttribute('style', 'position:absolute;width:0px;height:0px;left:500px;top:500px;');
        document.body.appendChild(iframe);
        doc = iframe.contentWindow.document;
        // 引入打印的专有CSS样式，根据实际修改
        // doc.write('<LINK rel="stylesheet" type="text/css" href="css/print.css">');
        doc.write('hello world');
        doc.close();
        // 获取iframe的焦点，从iframe开始打印
        iframe.contentWindow.focus();
        iframe.contentWindow.print();
        if (navigator.userAgent.indexOf("MSIE") > 0)
        {
            document.body.removeChild(iframe);
        }
    }
    return (
        <div>
            <h1>Hello</h1>
            <Button type='primary' onClick={print}>打印</Button>
            <div style={{ height: '100px', width: '100px', backgroundColor: 'red', display: 'none' }}>
                <h1>你好</h1>
            </div>
        </div>
    )
}